﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
using System.Web.UI;
using System.EnterpriseServices;
using System.ComponentModel;
using DAL;

namespace App.Models
{
    public class SupportFilter
    {
    }
    public class ActionPermission
    {
        /// <summary>
        /// 请求地址操作
        /// </summary>
        public virtual string ActionName { get; set; }

        /// <summary>
        /// 请求地址控制器
        /// </summary>
        public virtual string ControllerName { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public virtual string Description { get; set; }
    }
    public class ExceptionLogAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {


            //  sw.Write(filterContext.Exception.Message);


            base.OnException(filterContext);

            filterContext.HttpContext.Response.Redirect("/");
        }
    }


    public class SupportFilterAttribute : ActionFilterAttribute
    {
        static bool IsSubClassOf(Type type, Type baseType)
        {
            var b = type.BaseType;
            while (b != null)
            {
                if (b.Equals(baseType))
                { return true; }
                b = b.BaseType;
            }
            return false;
        }
        //private bool IsLogin = true;
        public void GetAllActionByAssembly()
        {
            var result = new List<ActionPermission>();
            SysEntities army = new SysEntities();


            var types = from t in Assembly.GetExecutingAssembly().GetTypes() where IsSubClassOf(t, typeof(App.Models.BaseController)) select t;
            var role = army.SysRole.Where(o => o.Id == "87aacded-7e48-455c-9de9-fdac77bfb44c").FirstOrDefault();
            foreach (var type in types)
            {
                if (type.BaseType.Name == "BaseController")//如果是Controller
                {
                    var members = type.GetMethods();
                    //var role = SysRole.CreateSysRole("87aacded-7e48-455c-9de9-fdac77bfb44c");
                    foreach (var member in members)
                    {
                        if (member.ReturnType.Name == "ActionResult" || member.ReturnType.Name == "JsonResult")//如果是Action
                        {

                            var promise = new SysPromise();
                            promise.Id = Guid.NewGuid().ToString();
                            promise.ActionName = member.Name;
                            promise.ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10); // 去掉“Controller”后缀
                            promise.Name = "clark";
                            promise.CreateTime = DateTime.Now;

                            //promise.SysRole.Where(o=>o.id);
                            //object[] attrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                            //if (attrs.Length > 0)
                            //    ap.Description = (attrs[0] as System.ComponentModel.DescriptionAttribute).Description;
                            //army.SysPromise.AddObject(promise);
                            role.SysPromise.Add(promise);
                        }

                    }
                }
            }
            army.SaveChanges();
            return;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //GetAllActionByAssembly();
            //var entity = new
            // {
            //     ActionName = filterContext.HttpContext.Request["ActionName"],
            //     ControllerName = filterContext.HttpContext.Request["ControllerName"],
            //     Description = filterContext.HttpContext.Request["Description"]
            // };

            //string controllerName = (string)filterContext.RouteData.Values["controller"];

            //string actionName = (string)filterContext.RouteData.Values["action"];



            ////if (IsLogin && filterContext.HttpContext.Session["Login_User"] == null)
            //{

            //    filterContext.HttpContext.Response.Redirect(new UrlHelper(filterContext.RequestContext).Action("Login", "Default"));

            //    filterContext.Result = new EmptyResult();

            //}

            //// else if (IsLogin && filterContext.HttpContext.Session["Login_User"] != null)
            //{

            //    //Mad_User user = filterContext.HttpContext.Session["Login_User"] as Mad_User;

            //    //if (!user.is_super)
            //    //{

            //    //    if (!GetPopedom(user).Exists(p => p.Controller_Name == controllerName.ToLower() && p.Action_Name == actionName.ToLower()))
            //    //    {

            //    //        filterContext.HttpContext.Response.Write("没有权限");
            //    //        filterContext.Result = new EmptyResult();

            //    //    }



            //    //}

            //}



        }




    }
}